﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Collections;
using System.Security.Cryptography;
using System.IO;

namespace WebAppMatrix.Extensions
{
    public static class StringExtensions
    {
        #region 删除前缀/后缀

        /// <summary>
        /// 删除前缀
        /// </summary>
        /// <param name="sourceString">原字符串</param>
        /// <param name="prefix">前缀</param>
        /// <param name="ignoreCase">是否区分大小写</param>
        /// <returns>删除前缀后的字符串</returns>
        public static string TrimPrefix(this string sourceString, string prefix, bool ignoreCase = false)
        {
            prefix = prefix ?? string.Empty;
            if (!sourceString.StartsWith(prefix, ignoreCase, MatrixManager.DefaultCultureInfo))
            {
                return sourceString;
            }

            return sourceString.Remove(0, prefix.Length);
        }

        /// <summary>
        /// 删除后缀
        /// </summary>
        /// <param name="sourceString">原字符串</param>
        /// <param name="suffix">后缀</param>
        /// <param name="ignoreCase">是否区分大小写</param>
        /// <returns>删除后缀后的字符串</returns>
        public static string TrimSuffix(this string sourceString, string suffix, bool ignoreCase = false)
        {
            suffix = suffix ?? string.Empty;
            if (!sourceString.EndsWith(suffix, ignoreCase, MatrixManager.DefaultCultureInfo))
            {
                return sourceString;
            }
            return sourceString.Substring(0, sourceString.Length - suffix.Length);
        }

        #endregion

        #region 是否为空

        /// <summary>
        /// 是否为null或空
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }

        public static bool IsNullOrWhiteSpace(this string s)
        {
            return string.IsNullOrWhiteSpace(s);
        }

        #endregion

        #region 拼接字符串

        /// <summary>
        /// 把集合拼接成一个字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="separator">分隔符</param>
        /// <returns></returns>
        public static string JoinWith(this IEnumerable source, string separator)
        {
            StringBuilder strBuilder = new StringBuilder();
            string rst = string.Empty;

            foreach (var item in source)
            {
                strBuilder.Append(item.ToString());
                strBuilder.Append(separator);
            }
            rst = strBuilder.ToString();

            if (!rst.IsNullOrEmpty())
                rst = rst.TrimSuffix(separator);

            return rst;
        }

        #endregion

        #region 汉字金额转换

        /// <summary>
        /// 转换为大写人民币金额
        /// </summary>
        /// <param name="money"></param>
        /// <returns></returns>
        public static string ToChineseMoney(this decimal money)
        {
            string stringMoney = money.ToString("#.##");
            if (stringMoney.Length == 0)
            {
                stringMoney = "0";
            }
            string output = string.Empty;
            string[] scales = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟" };
            string[] bases = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

            string part1 = stringMoney.ToString();
            string part2 = string.Empty;
            //计算小数点前面的部分
            if (stringMoney.IndexOf(".") > -1)
            {
                part1 = stringMoney.Substring(0, stringMoney.IndexOf("."));
                part2 = stringMoney.Substring(stringMoney.IndexOf(".") + 1, stringMoney.Length - stringMoney.IndexOf(".") - 1);
            }
            for (int i = part1.Length; i > 0; i--)
            {
                int count = Convert.ToInt16(part1[part1.Length - i]);
                output += bases[count - 48];
                output += scales[i + 1];
            }
            //计算小数点后面的部分
            for (int i = 0; i < part2.Length; i++)
            {
                int count = Convert.ToInt16(part2[i]);
                output += bases[count - 48];
                if (i == 0)
                {
                    output += "角";
                }
                if (i == 1)
                {
                    output += "分";
                }
            }
            return output;
        }

        #endregion
    }
}
